package com.jxwifi.cloud.quickclean.utils;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.widget.Toast;

import com.jxwifi.cloud.quickclean.R;
import com.jxwifi.cloud.quickclean.app.Host;
import com.jxwifi.cloud.quickclean.app.StateUtils;
import com.tencent.mm.opensdk.modelmsg.SendMessageToWX;
import com.tencent.mm.opensdk.modelmsg.WXMediaMessage;
import com.tencent.mm.opensdk.modelmsg.WXMiniProgramObject;
import com.tencent.mm.opensdk.modelmsg.WXWebpageObject;
import com.tencent.mm.opensdk.openapi.IWXAPI;

import java.io.ByteArrayOutputStream;

public class shareUtils {

    public static void  share2weixin(IWXAPI iwxapi, int flag, Context mContext,int shareType,String orderCode,String cdesk){
        if (!iwxapi.isWXAppInstalled()) {
            Toast.makeText(mContext, "您还未安装微信客户端",
                    Toast.LENGTH_SHORT).show();
            return;
        }
        if(shareType == 5){//分享网址
            WXWebpageObject webpage = new WXWebpageObject();
            webpage.webpageUrl = "http://baidu.com";
            WXMediaMessage msg = new WXMediaMessage(webpage);
            msg.title = "标题";
            msg.description = mContext.getResources().getString(R.string.app_name);
            Bitmap thumb = BitmapFactory.decodeResource(mContext.getResources(),
                    R.mipmap.voucher);
            msg.setThumbImage(thumb);
            SendMessageToWX.Req req = new SendMessageToWX.Req();
            req.transaction = String.valueOf(System.currentTimeMillis());
            req.message = msg;
            if (flag==1){
                req.scene = SendMessageToWX.Req.WXSceneSession;//好友
            }else if (flag==0){
                req.scene = SendMessageToWX.Req.WXSceneTimeline; //朋友圈
            }else{
                req.scene = SendMessageToWX.Req.WXSceneTimeline; //朋友圈
            }
            iwxapi.sendReq(req);

        }else if(shareType == 6){//分享小程序
            WXMiniProgramObject miniProgramObj = new WXMiniProgramObject();
            miniProgramObj.webpageUrl = "http://www.qq.com"; // 兼容低版本的网页链接
            if (StateUtils.HOST_ON_LINE.equals(Host.HOST)){
                miniProgramObj.miniprogramType = WXMiniProgramObject.MINIPTOGRAM_TYPE_RELEASE;// 正式版:0，测试版:1，体验版:2
            }else{
                miniProgramObj.miniprogramType = WXMiniProgramObject.MINIPROGRAM_TYPE_PREVIEW;// 正式版:0，测试版:1，体验版:2
            }
            miniProgramObj.userName = StateUtils.WEIXIN_XIAOCHENGXU_ID;// 小程序原始id
            miniProgramObj.path = "/packageA/pages/assemble?joinid=3&orderCode="+orderCode+"&cdesk="+cdesk;            //小程序页面路径；对于小游戏，可以只传入 query 部分，来实现传参效果，如：传入 "?foo=bar"

            WXMediaMessage msg = new WXMediaMessage(miniProgramObj);
            msg.title = "拼团邀请";                    // 小程序消息title
            msg.description = "拼团邀请";               // 小程序消息desc
            Bitmap bitmap = BitmapFactory.decodeResource(mContext.getResources(),R.mipmap.share_group_wxmini);
            Bitmap sendBitmap = Bitmap.createScaledBitmap(bitmap,200,200,true);
            bitmap.recycle();
            msg.thumbData = bmpToByteArray(sendBitmap,true);             // 小程序消息封面图片，小于128k

            SendMessageToWX.Req req = new SendMessageToWX.Req();
            req.transaction = buildTransaction("miniProgram");
            req.message = msg;
            req.scene = SendMessageToWX.Req.WXSceneSession;  // 目前只支持会话
            iwxapi.sendReq(req);
        }

    }

    private static String buildTransaction(final String type) {
        return (type == null) ? String.valueOf(System.currentTimeMillis()) : type + System.currentTimeMillis();
    }

    /**
     * Bitmap转Byte[]
     * @param bmp
     * @param needRecycle
     * @return
     */
    public static byte[] bmpToByteArray(final Bitmap bmp, final boolean needRecycle) {
        ByteArrayOutputStream output = new ByteArrayOutputStream();
        bmp.compress(Bitmap.CompressFormat.PNG, 100, output);
        if (needRecycle) {
            bmp.recycle();
        }

        byte[] result = output.toByteArray();
        try {
            output.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }
}
